//
//  LoginViewController.swift
//  ZTC_Final
//
//  Created by xy Man on 2022/5/24.
//

import UIKit

class LoginViewController: UIViewController {
    @IBOutlet weak var loginName: UITextField!
    
    @IBOutlet weak var loginPassword: UITextField!
    
    @IBOutlet weak var status: UILabel!
    
    var successfullName:String = "none"
    var successfullPassword:String = "none"
    var success:Bool = false
    // 用户偏好
    let defaults = UserDefaults.standard
    
    @IBAction func ogin(_ sender: Any) {

        
        // 判断有没有值
        let name = loginName.text!
        let password = loginPassword.text!
        if name.count == 0 || password.count == 0{
            status.text = "请完整输入"
            return
        }
        // 用户名是否存在
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let sql = "SELECT * FROM user;"
        let res = sqlite.execQuerySQL(sql: sql)
        for i in res!{
            let tempName = i["userName"] as! String
            let tempPassword = i["userPassword"] as! String
            if tempName == name && tempPassword == password{
                successfullName = tempName
                successfullPassword = tempPassword
                success = true
                break
            }
        }
        if success{
            print(successfullName)
            // 保存偏好
            defaults.set(successfullName, forKey: "lastName")
//            performSegue(withIdentifier: "login2main", sender: nil)
             //执行转场
            let mainBoard:UIStoryboard!=UIStoryboard(name:
            "Main", bundle: nil)
            let VCMain =
            mainBoard!
            .instantiateViewController(withIdentifier:
            "VCMain") as! MainTabViewController
            VCMain.username = successfullName
                    dismiss (animated: true, completion: {(UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows[0].rootViewController=VCMain})

        }
        status.text = "用户名或者密码错误"
        return
        
        // 执行转场
//        let mainBoard:UIStoryboard!=UIStoryboard(name:
//        "Main", bundle: nil)
//        let VCMain =
//        mainBoard!
//        .instantiateViewController(withIdentifier:
//        "VCMain")

//        self.dismiss(animated: false)
        
        
//        dismiss (animated: true, completion: {(UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows[0].rootViewController=VCMain})

        
    }
    
    func initUserTable()->Void{
        // 初始化数据库
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        //创建书籍信息表
        let createUserTable = "CREATE TABLE IF NOT exists user( 'userName' primary key, 'userPassword' text);"
        if !sqlite.execNoneQuerySQL(sql: createUserTable){sqlite.closeDB(); return}
        else{
            print("用户库建库成功")
        }
    }
    
//    func testUserTable()->Void{
//        // 初始化数据库
//        let sqlite = SQLiteManager.sharedInstance
//
//        if !sqlite.openDB(){return}
//        //创建书籍信息表
//        let testsql = "REPLACE INTO user VALUES('123456', '123456')"
//        if !sqlite.execNoneQuerySQL(sql: testsql){sqlite.closeDB(); return}
//        else{
//
//        }
//        let testsql2 = "SELECT * FROM user"
//        if !sqlite.openDB(){return}
//        let query = sqlite.execQuerySQL(sql: testsql2)
//        print(query)
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getBooksFromURL()
        initUserTable()
        loginName.text = defaults.string(forKey: "lastName")
    }
        
//        testUserTable()
        // Do any additional setup after loading the view.
    
    

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
//    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//        let mainTab = segue.destination as? MainTabViewController
//        if mainTab == nil{
//            return
//        }
//        mainTab!.username = successfullName
//        // Get the new view controller using segue.destination.
//        // Pass the selected object to the new view controller.
//    }
    


}
